package com.cec.config;

import com.cec.interceptor.CorsInterceptor;
import com.cec.interceptor.JwtTokenAdminInterceptor;
import com.cec.interceptor.JwtTokenUserInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;
    @Autowired
    private CorsInterceptor corsInterceptor;

    //处理跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                //.allowCredentials(true)
                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS","PATCH")
                .maxAge(3600);
    }

    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截同源拦截器
        registry.addInterceptor(corsInterceptor)
                .addPathPatterns("/**");

        // 管理员相关请求拦截
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/test/**")
                .addPathPatterns("/admin/**")
                //.addPathPatterns("/testException")
                .excludePathPatterns("/admin/login");

        // 用户端请求拦截
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/test/**")
                .addPathPatterns("/user/**")
                //.addPathPatterns("/testException")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register");
    }

    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket docket1() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("社区养老服务接口文档")
                .version("1.0")
                .description("社区养老服务接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cec.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    /*@Bean
    public Docket docket2() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("社区养老服务接口文档")
                .version("1.0")
                .description("社区养老服务接口文档")
                .build();
        Docket docket1 = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cec.controller.user"))
                .paths(PathSelectors.any())
                .build();
        return docket1;
    }*/

    /**
     * 设置静态资源映射
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
